//通过文件路径截取名称
function subFileName(fileUrl) {
    var index = fileUrl.lastIndexOf("\/");
    fileUrl = fileUrl.substring(index + 1, fileUrl.length);
    return fileUrl
}

function levelBack() {
    window.history.back(-1);
}

function ajaxToken(url, type, data, success, fail) {
    var is_mobi = navigator.userAgent.toLowerCase().match(/(ipod|iphone|android|coolpad|mmp|smartphone|midp|wap|xoom|symbian|j2me|blackberry|wince)/i) != null;
    $.ajax({
        url: url,
        type: type,
        data: data,
        beforeSend: function (request) {
            request.setRequestHeader("Authorization", window.localStorage.getItem("token"))
        },
        success: function (res) {
            console.log(res.code);
           if (res.code === 0 || res.code === '0') {
             success && success(res.data);
            } else if (res.code === 10000 || res.code === '10000') {
                // 登录态失效, 重新登录

                location.href = "/cultivate/cultivateLogins?name=0"
            } else if (res.msg == "暂无试卷，敬请期待" || res.msg == "已经参加过该考试！" || res.msg == "考试时间还未到！" || res.msg == "已超过考试截止时间！") {
                layer.confirm(res.msg, {
                    closeBtn: 0,
                    btn: ['返回'],
                    yes: function (index, layer) {
                        window.history.back(-1);
                    }
                });
            }else if (res.code == 40023 && is_mobi){
               layer.confirm('您已提交过此课程，请前往个人中心—我的订单支付或上课！', {
                   btn: ['确定','取消']
               }, function(){

                   location.href = '/cultivate/my'
               });

           }else {
                layer.alert(res.msg , "223");
                return false;
            }
        },
        error: function (res) {
            console.log(res.status)
            if (res.status == 401 && is_mobi) {
                // 登录态失效, 重新登录
                location.href = '/cultivate/mobileLogin?name=0'

            } else {
                location.href = "/cultivate/cultivateLogins?name=0"
                fail && fail(res);
                layer.alert(res.errMsg);
            }
        }
    });
}

function ajaxTokenForm(url, type, data, success, fail) {
    $.ajax({
        url: url,
        type: type,
        data: data,
        beforeSend: function (request) {
            request.setRequestHeader("Authorization", window.localStorage.getItem("token"))
        },
        success: function (res) {
            //console.log("111", res);
            if (res.code === 0 || res.code === '0') {
                layer.confirm('提交成功', {
                    closeBtn: 0,
                    btn: ['返回'],
                    yes: function (index, layero) {
                        // window.history.back(-1);
                        location.href=document.referrer
                    }
                });
                success && success(res.data);
                //
            } else if (res.code === 10000 || res.code === '10000') {
                // 登录态失效, 重新登录
                location.href ="/cultivate/cultivateLogins?name=0";
            } else {
                layer.alert(res.msg);
                return false;
            }
        },
        error: function (res) {
            if (res.status == 401) {
                // 登录态失效, 重新登录
                location.href = "/cultivate/cultivateLogins?name=0";
            } else {
                fail && fail(res);
                layer.alert(res.errMsg);
            }
        }
    });
}

function formatTime(date) {
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var day = date.getDate();

    var hour = date.getHours();
    var minute = date.getMinutes();
    var second = date.getSeconds();
    return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

// 个位数的话在十位数补0
function formatNumber(n) {
    n = n.toString();
    return n[1] ? n : '0' + n
}